package com.wgc.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wgc.goods.domain.Spec;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgc.goods.service.SpecService;
import com.wgc.goods.mapper.SpecMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 15776
 * @description 针对表【goods_spec(规格表)】的数据库操作Service实现
 * @createDate 2024-07-27 11:24:17
 */
@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec>
        implements SpecService {
    @Autowired
    private SpecMapper specMapper;

    @Override
    public List<Spec> selectAll() {
        LambdaQueryWrapper<Spec> lqw = new LambdaQueryWrapper<>();
         lqw.eq(Spec::getIsDelete,0);
        return specMapper.selectList(lqw);
    }

    @Override
    public Integer insert(Spec spec) {
        return specMapper.insert(spec);
    }

    @Override
    public Integer update(Spec spec) {
        return specMapper.updateById(spec);
    }

    @Override
    public Integer delete(String ids) {
       try {
            String[] split = ids.split(",");
            for (String id : split) {
                Spec spec = new Spec();
                spec.setId(Long.parseLong(id));
                spec.setIsDelete(1);
                specMapper.updateById(spec);
            }
            return 1;
        }catch (Exception e){
           return -1;
       }
    }

    @Override
    public Spec selectById(Integer id) {
        return null;
    }

    @Override
    public List<Spec> selectList(Spec spec) {
        LambdaQueryWrapper<Spec> lqw = this.lqw(spec);
        return specMapper.selectList(lqw);
    }

    @Override
    public Page<Spec> findPage(Integer page, Integer size) {
        Page<Spec> page1 = new Page<>(page,size);
        LambdaQueryWrapper<Spec> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Spec::getIsDelete,0);
        return this.page(page1,lqw);
    }

    @Override
    public Page<Spec> findPage(Spec spec, Integer page, Integer size) {
        Page<Spec> page1 = new Page<>(page,size);
        return this.page(page1,lqw(spec));
    }

    public LambdaQueryWrapper<Spec> lqw(Spec iSpec){
        LambdaQueryWrapper <Spec> lqw = new LambdaQueryWrapper<>();
        if (iSpec.getId() != null){
            lqw.eq(Spec::getId,iSpec.getId());
        }
        if (iSpec.getSpecName() != null){
            lqw.eq(Spec::getSpecName,iSpec.getSpecName());
        }
        if (iSpec.getSpecValue() != null){
            lqw.eq(Spec::getSpecValue,iSpec.getSpecValue());
        }
        if (iSpec.getIsDelete() != null){
            lqw.eq(Spec::getIsDelete,iSpec.getIsDelete());
        }
        if (iSpec.getCreateTime() != null){
            lqw.eq(Spec::getCreateTime,iSpec.getCreateTime());
        }
        if (iSpec.getUpdateTime() != null){
            lqw.eq(Spec::getUpdateTime,iSpec.getUpdateTime());
        }
        return lqw;
    }

}




